class Solution:
    def canFinish(self, numCourses: int, prerequisites) -> bool:
        points = [set() for _ in range(numCourses)]
        dic = {}
        stack = set()
        for x, y in prerequisites:
            points[x].add(y)
            if y in dic:
                dic[y] = dic[y] + 1
            else:
                dic[y] = 1
            if y in stack:
                stack.remove(y)
            if x not in dic and x not in stack:
                stack.add(x)
        while stack and dic:
            p = stack.pop()
            for i in points[p]:
                dic[i] = dic[i] - 1
                if dic[i] == 0:
                    dic.pop(i)
                    stack.add(i)

        return len(dic) == 0


if __name__ == '__main__':
    so = Solution()
    s = 2
    t = [[1,0],[0,1]]
    r = so.canFinish(s, t)
    print(r)
